package cn.gdrj.est.service.impl;

import cn.gdrj.est.constants.Constants;
import cn.gdrj.est.constants.ResultEnum;
import cn.gdrj.est.dao.UsersDao;
import cn.gdrj.est.entity.Users;
import cn.gdrj.est.exception.BusinessException;
import cn.gdrj.est.service.SmsVerifyCodeService;
import cn.gdrj.est.service.UsersService;
import cn.gdrj.est.vo.TokenVO;
import cn.hutool.core.lang.UUID;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

@Service
public class UsersServiceImpl implements UsersService {
    @Autowired
    private UsersDao usersDao;
    @Autowired
    private SmsVerifyCodeService smsVerifyCodeService;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public Users findById(Long id) {
        return usersDao.selectByPrimaryKey(id);
    }

    @Override
    public Users findByOpenId(String openid) {
        return usersDao.selectByOpenId(openid);
    }

    @Override
    public Users register(Users users) {
        usersDao.insertSelective(users);
        return users;
    }

    @Override
    public TokenVO login(Users users) {

        return null;
    }

    @Override
    public Users loginBySms(String phone, String sms) {
        // 根据手机号获取验证码
        String loginVerifyCode =
                smsVerifyCodeService.getLoginVerifyCode(phone);
        // 对比验证码
        // 如果验证码不正确，抛出异常
        if(Objects.isNull(loginVerifyCode) ||
                !Objects.equals(loginVerifyCode,sms)){
            throw new BusinessException(ResultEnum.FAIL_VERIFY);
        }
        // 验证码正确的时候，删除验证码
        smsVerifyCodeService.removeLoginVerifyCode(phone);

        // 到数据库中查询用户信息是否存在
        Users users = usersDao.selectByAccount(phone);

        // 如果不存在，就自动注册
        if(Objects.isNull(users)){
            users = new Users();
            users.setAccount(phone);
            users.setUserName(phone);
            users.setIsAuthentication(Constants.EST_NOT);
            Date createdTime = new Date();
            users.setCreatedTime(createdTime);
            users.setUpdatedTime(createdTime);
            users.setIsDelete(Constants.EST_NOT);
            usersDao.insertSelective(users);
        }
        return users;
    }

//    @Override
//    public TokenVO generateToken(Users users) {
//        TokenVO tokenVO= new TokenVO();
//        // 生成token
//        String uuid = UUID.fastUUID().toString(true);
//        tokenVO.setToken(uuid);
//        // 保存会话信息
//        ValueOperations<String, String> valueOperations =
//                stringRedisTemplate.opsForValue();
//        String usersJsonString = JSON.toJSONString(users);
//        valueOperations.set(Constants.Redis.PREFIX_TOKEN+uuid,usersJsonString,60, TimeUnit.MINUTES);
//        return tokenVO;
//    }
}
